package com.ltmonitor.web.controller.command;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ltmonitor.entity.MapArea;
import com.ltmonitor.entity.MapAreaBinding;
import com.ltmonitor.entity.TerminalCommand;
import com.ltmonitor.service.IMapAreaBindingService;
import com.ltmonitor.service.IMapAreaService;
import com.ltmonitor.service.JT808Constants;
import com.ltmonitor.web.util.JsonMessage;

/**
 * 围栏区域下发给终端或者平台的命令
 * 根据808协议文档，下发电子围栏到终端。
 * @author DELL
 * 
 */
@Controller
@RequestMapping("/bindMapArea")
public class BindMapAreaAction extends TerminalCommandAction {


	@Autowired
	private IMapAreaBindingService mapAreaBindingService;

	@Autowired
	private IMapAreaService mapAreaService;


	/**
	 * 打开配置绑定车辆区域页面
	 * @param vehicleId
	 * @param m
	 * @return
	 */
	@RequestMapping("/viewBindMapArea.action")
	public String view(long vehicleId, Model m) {
		m.addAttribute("vehicleId", vehicleId);
		return "/command/mapAreaList";
	}
	

	/**
	 * 打开配置绑定线路页面
	 * @param vehicleId
	 * @param m
	 * @return
	 */
	@RequestMapping("/viewBindRoute.action")
	public String viewBindRoute(long vehicleId, Model m) {
		m.addAttribute("vehicleId", vehicleId);
		return "/command/routeList";
	}


	/**
	 * 区域和车辆绑定，并下发给终端
	 * @param vehicleId
	 * @param areaIds 区域id字符串，以逗号隔开
	 * @param bindType 绑定类型，绑定给平台或者终端
	 * @param configType 配置类型 0：更新区域； 1：追加区域； 2：修改区域 ；3 删除单个区域
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/bind.action")
	public JsonMessage  execute(long vehicleId,String areaIds,String bindType, int configType) {
		if (areaIds == null) {
			// this.setMessage("请选择要下发的区域");
			return json(false, "请选择要下发的区域或线路!");
		}
		long commandId = 0;
		try {
			String[] areaId = areaIds.split(",");
			for (String strId : areaId) {
				long id = Long.parseLong(strId);
				MapArea ec = mapAreaService
						.load( id);
				MapAreaBinding eb = getBinding(id, vehicleId);
				if (bindType.equals(MapAreaBinding.BINDING_TERMINAL)) {
					TerminalCommand tc = createCommand(ec, configType, vehicleId);
					eb.setCommandId(tc.getEntityId());
					commandId = tc.getEntityId();
				}else if(configType == 3)
				{
					//configType = 0;
					bindType = null;
				}
				eb.setConfigType(configType);
				eb.setBindType(bindType);
				this.mapAreaBindingService.saveOrUpdate(eb);
			}
		} catch (Exception ex) {
			log.error(ex.getMessage(),ex);
			return json(false, ex.getMessage());
		}
		return json(true, commandId);
	}

	private MapAreaBinding getBinding(long mapAreaId, long vehicleId) {
		MapAreaBinding eb = this.mapAreaBindingService.getMapAreaBinding(mapAreaId, vehicleId);
		if (eb == null) {
			eb = new MapAreaBinding();
			eb.setAreaId(mapAreaId);
			eb.setVehicleId(vehicleId);
		}
		return eb;
	}

	private List<MapAreaBinding> getAllBinding(long vehicleId,
			String areaType) {
		String hql = "from MapAreaBinding where  vehicleId = ? and bindType = ?";

		List<MapAreaBinding> ebList = this.mapAreaBindingService.getAllBinding(vehicleId, MapAreaBinding.BINDING_TERMINAL );
		List<MapAreaBinding> result = new ArrayList<MapAreaBinding>();
		for (int m = 0; m < ebList.size(); m++) {
			MapAreaBinding eb = (MapAreaBinding) ebList.get(m);
			MapArea ec = (MapArea) this.mapAreaService.load(eb.getAreaId());

			if (ec.getAreaType().equals(areaType)) {
				result.add(eb);
			}

		}
		return result;
	}

	/**
	 * 删除终端的某一区域类型的所有区域
	 * @param vehicleId
	 * @param areaType
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/deleteAllBinding.action")
	public JsonMessage  deleteAllBinding(long vehicleId,String areaType) {
		try {
			long commandId = 0;
			TerminalCommand tc = new TerminalCommand();
			tc.setVehicleId(vehicleId);
			tc.setCmdData("");
			// tc.setCmd("" + configType);
			if (MapArea.CIRCLE.equals(areaType)) {
				tc.setCmdType(JT808Constants.CMD_DELETE_CIRCLE);
			} else if (MapArea.RECT.equals(areaType)) {
				tc.setCmdType(JT808Constants.CMD_DELETE_RECT);
			} else if (MapArea.POLYGON.equals(areaType)) {
				tc.setCmdType(JT808Constants.CMD_DELETE_POLYGON);
			} else if (MapArea.ROUTE.equals(areaType)) {
				tc.setCmdType(JT808Constants.CMD_DELETE_ROUTE);
			}

			SendCommand(tc);
			commandId = tc.getEntityId();
			List<MapAreaBinding> ebList = getAllBinding(vehicleId,
					areaType);
			for (MapAreaBinding eb : ebList) {
				eb.setCommandId(tc.getEntityId());
				eb.setConfigType(3);
			}
			this.mapAreaBindingService.saveOrUpdateAll(ebList);

			return json(true, commandId);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			return json(false, ex.getMessage());
		}
	}

	private TerminalCommand createCommand(MapArea ec, int configType, long vehicleId) {

		TerminalCommand tc = new TerminalCommand();
		tc.setVehicleId(vehicleId);
		tc.setCmdData("" + ec.getEntityId());
		tc.setCmd("" + configType);
		if (MapArea.CIRCLE.equals(ec.getAreaType())) {
			tc.setCmdType(JT808Constants.CMD_CIRCLE_CONFIG);
			if (configType == 3) {
				tc.setCmdType(JT808Constants.CMD_DELETE_CIRCLE);
			}
		} else if (MapArea.RECT.equals(ec.getAreaType())) {
			tc.setCmdType(JT808Constants.CMD_RECT_CONFIG);
			if (configType == 3) {
				tc.setCmdType(JT808Constants.CMD_DELETE_RECT);
			}
		} else if (MapArea.POLYGON.equals(ec.getAreaType())) {
			tc.setCmdType(JT808Constants.CMD_POLYGON_CONFIG);
			if (configType == 3) {
				tc.setCmdType(JT808Constants.CMD_DELETE_POLYGON);
			}
		} else if (MapArea.ROUTE.equals(ec.getAreaType())) {
			tc.setCmdType(JT808Constants.CMD_ROUTE_CONFIG);
			if (configType == 3) {
				tc.setCmdType(JT808Constants.CMD_DELETE_ROUTE);
			}
		}

		SendCommand(tc);
		return tc;

	}

	



}
